import {
  ClassicModel
} from '../../models/classic.js';
import {
  LikeModel
} from '../../models/like.js';
let classicModel = new ClassicModel();
let likeModel = new LikeModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openType: '',
    classicData: null,
    latest: true,
    fisrst: false,
    likeCount: 0,
    likeStatus: false,
    play: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    classicModel.getLatest()
      .then(res => {
        this.setData({
          classicData: res,
          likeCount: res.fav_nums,
          likeStatus: res.like_status
        });
        // 写入缓存中
        let key = this._getKey(res.index);
        wx.setStorageSync(key, res);
        this._setLatestIndex(res.index);
      });
    // classicModel.getLatest(res => {
    //   this.setData({
    //     classicData: res,
    //     likeCount: res.fav_nums,
    //     likeStatus: res.like_status
    //   });
    // });

  },

  _getLikeStatus(arrID, category) {
    likeModel.getClassicLikeStatus(arrID, category)
      .then(res => {
        this.setData({
          likeCount: res.fav_nums,
          likeStatus: res.like_status
        })
      })
    // likeModel.getClassicLikeStatus(arrID, category, res => {
    //   this.setData({
    //     likeCount: res.fav_nums,
    //     likeStatus: res.like_status
    //   })
    // });
  },

  onLike(event) {
    let td = this.data,
      behavior = event.detail.behavior,
      artID = td.classicData.id,
      category = td.classicData.type;
    likeModel.like(behavior, artID, category);
  },

  onNext(e) {

    this._updateClassic('next');
  },

  onPrevious(e) {

    this._updateClassic('previous');
  },

  _updateClassic(nextOrPrevious, behavior) {
    let index = this.data.classicData.index;
    let key = nextOrPrevious == 'next' ? this._getKey(index + 1) : this._getKey(index - 1);
    // 从缓存中获取
    let classic = wx.getStorageSync(key);
    if (!classic) {
    classicModel.getClassic(index, nextOrPrevious)
      .then(res => {
        wx.setStorageSync(this._getKey(res.index), res);

        this.setData({
          classicData: res,
          latest: classicModel.isLatest(res.index),
          first: classicModel.isFirst(res.index),
          play: true
        });

        this._getLikeStatus(res.id, res.type);
      });
    } else {
      this.setData({
        classicData: classic,
        latest: classicModel.isLatest(classic.index),
        first: classicModel.isFirst(classic.index),
        play: true
      });
      this._getLikeStatus(classic.id, classic.type);
    }

    // classicModel.getClassic(index, nextOrPrevious, (res) => {
    //   this.setData({
    //     classicData: res,
    //     latest: classicModel.isLatest(res.index),
    //     first: classicModel.isFirst(res.index),
    //     play: true
    //   });

    //   this._getLikeStatus(res.id, res.type);
    // });
  },

  _setLatestIndex(index) {
    wx.setStorageSync('latest', index);
  },

  _getLatestIndex() {
    let index = wx.getStorageSync('latest');
    return index;
  },

  _getKey(index) {
    let key = `classic-${index}`;
    return key;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})